{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "patientDirectory = \"G:/My Drive/Projects/Proj_UNC_StrokeCollaterals/Experiments/UNC/CTP/CTAT-001\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import itk\n",
    "from itk import TubeTK as ttk\n",
    "\n",
    "import vtk\n",
    "\n",
    "import itkwidgets\n",
    "from itkwidgets import view"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "import ipywebrtc as webrtc\n",
    "\n",
    "from reportlab.pdfgen.canvas import Canvas\n",
    "from reportlab.lib.pagesizes import letter, inch\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "vtpFileName = patientDirectory + \"-PerfusionMaps/VesselsPerf.vtp\"\n",
    "reader = vtk.vtkXMLPolyDataReader()\n",
    "reader.SetFileName(vtpFileName)\n",
    "reader.Update()\n",
    "vessVtp = reader.GetOutput()\n",
    "\n",
    "img = itk.imread(patientDirectory + \"-MinMax/max3.nrrd\", itk.F)\n",
    "\n",
    "imgCBF = itk.imread(patientDirectory + \"-PerfusionMaps/CBF.nii\", itk.F)\n",
    "imgTMax = itk.imread(patientDirectory + \"-PerfusionMaps/Tmax.nii\", itk.F)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Viewer(background=(0.5, 0.5, 0.5), cmap=['Viridis (matplotlib)'], geometries=[{'vtkClass': 'vtkPolyData', 'poi…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "view1 = view(image=imgTMax, geometries=vessVtp, cmap=itkwidgets.cm.viridis, gradient_opacity=0.9, background=[0.5,0.5,0.5], slicing_planes=True)\n",
    "view1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "image_recorder1 = webrtc.ImageRecorder(stream=view1, filename=patientDirectory + '-PerfusionMaps/Report-CBF.png', autosave=True)\n",
    "image_recorder1.recording=True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "568c8ec88e8646b1883c00b8f7ce9f8f",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Viewer(background=(0.5, 0.5, 0.5), cmap=['Viridis (matplotlib)'], geometries=[{'vtkClass': 'vtkPolyData', 'poi…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "view2 = view(image=img, geometries=vessVtp, cmap=itkwidgets.cm.viridis, gradient_opacity=0.9, camera=view1.camera, background=[0.5,0.5,0.5])\n",
    "view2\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "image_recorder2 = webrtc.ImageRecorder(stream=view2, filename=patientDirectory + '-PerfusionMaps/Report-CBV.png', autosave=True)\n",
    "image_recorder2.recording=True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "db37ed71c86f43b089e50c1e97b47c9b",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Viewer(background=(0.5, 0.5, 0.5), cmap=['Viridis (matplotlib)'], geometries=[{'vtkClass': 'vtkPolyData', 'poi…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "view3 = view(image=img, geometries=vessVtp, cmap=itkwidgets.cm.viridis, gradient_opacity=0.9, camera=view1.camera, background=[0.5,0.5,0.5])\n",
    "view3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "image_recorder3 = webrtc.ImageRecorder(stream=view3, filename=patientDirectory + '-PerfusionMaps/Report-TTP.png', autosave=True)\n",
    "image_recorder3.recording=True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "b437039c5cc244f3acf04a8a5369cd0c",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Viewer(background=(0.5, 0.5, 0.5), cmap=['Viridis (matplotlib)'], geometries=[{'vtkClass': 'vtkPolyData', 'poi…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "view4 = view(image=img, geometries=vessVtp, cmap=itkwidgets.cm.viridis, gradient_opacity=0.9, camera=view1.camera, background=[0.5,0.5,0.5])\n",
    "view4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "image_recorder4 = webrtc.ImageRecorder(stream=view4, filename=patientDirectory + '-PerfusionMaps/Report-TMax.png', autosave=True)\n",
    "image_recorder4.recording=True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "from datetime import datetime\n",
    "\n",
    "canvas = Canvas(patientDirectory + '-Report.pdf',pagesize=letter)\n",
    "\n",
    "canvas.setFont(\"Helvetica\", 10)\n",
    "canvas.drawString(0.5 * inch, 10 * inch, \"Patient Data:\")\n",
    "canvas.setFont(\"Courier\", 10)\n",
    "canvas.drawString(1.75 * inch, 10 * inch, patientDirectory)\n",
    "\n",
    "curTime = datetime.now().strftime(\"%d/%m/%Y %H:%M:%S\")\n",
    "canvas.setFont(\"Helvetica\", 10)\n",
    "canvas.drawString(0.5 * inch, 9.75 * inch, \"Process Date:\")\n",
    "canvas.setFont(\"Courier\", 10)\n",
    "canvas.drawString(1.75 * inch, 9.75 * inch, curTime)\n",
    "canvas.setFont(\"Helvetica\", 10)\n",
    "canvas.drawString(0.5 * inch, 9.5 * inch, \"TubeTK Version:\")\n",
    "canvas.setFont(\"Courier\", 10)\n",
    "canvas.drawString(1.75 * inch, 9.5 * inch, \"2011.12.04\")\n",
    "\n",
    "canvas.setFont(\"Helvetica\", 12)\n",
    "canvas.drawImage(patientDirectory + '-PerfusionMaps/Report-CBF.png', 0.5*inch, 6*inch, width=3.5*inch, height=2.5*inch)\n",
    "canvas.drawString(1 * inch, 8.75 * inch, \"CBF\")\n",
    "canvas.drawImage(patientDirectory + '-PerfusionMaps/Report-CBV.png', 4.25*inch, 6*inch, width=3.5*inch, height=2.5*inch)\n",
    "canvas.drawString(4.5 * inch, 8.75 * inch, \"CBV\")\n",
    "canvas.drawImage(patientDirectory + '-PerfusionMaps/Report-TTP.png', 0.5*inch, 1*inch, width=3.5*inch, height=2.5*inch)\n",
    "canvas.drawString(1 * inch, 3.75 * inch, \"TTP\")\n",
    "canvas.drawImage(patientDirectory + '-PerfusionMaps/Report-TMax.png', 4.25*inch, 1*inch, width=3.5*inch, height=2.5*inch)\n",
    "canvas.drawString(4.5 * inch, 3.75 * inch, \"TMax\")\n",
    "canvas.showPage()\n",
    "canvas.save()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
